<?php
namespace app\admin\controller;

use app\common\controller\AdminController;
use app\common\logic\AdminLogic;
use think\facade\Request;

class Admin extends AdminController
{
    public function initialize(){
        $this->modelName = 'Admin';
        $this->controllerName = '人员';
        $this->noCheckAction = ['profile'];
        parent::initialize();
    }
    protected function handle_add_get(&$param){
        $alj = new AdminLogic('AuthGroup');
        $this->assign('authGroupList',$alj->_getListByWhere([]));

        return ['err_code' => 0 , 'msg' => '默认通过'];
    }
    /**
     * 添加 保存 前
     * @param $param
     * @return array
     */
    protected function handle_add_post_before(&$param){
        // 加密密码
        $admin_pwd_salt = random(10);
        $param['admin_pwd_salt'] = $admin_pwd_salt;
        $param['admin_pwd'] = encrypt_password($param['admin_pwd'],$admin_pwd_salt);
        $param['admin_ip'] = request()->ip();
        $param['project_id'] = $this->getProjectId();
        $param['admin_addtime'] = time();
        $param['admin_changepwd'] = time();

        return ['err_code' => 0 , 'msg' => '默认通过'];
    }
    /**
     * 添加 保存 后
     * @param $flag
     * @return array
     */
    protected function handle_add_post_after(&$param,&$flag){
        if($flag['err_code'] === 0){
            // 添加成功 添加分组数据
            $alj = new AdminLogic();
            $jieguo = $alj->_setAuthGroupAccess($flag['data']['id'],$param['group_id']);
            if($jieguo === false){
                return ['err_code' => 1 , 'msg' => '设置分组出错'];
            }
        }
        return ['err_code' => 0 , 'msg' => '默认通过'];
    }

    /**
     * 编辑 获取 model 后
     * @param $model
     */
    protected function handle_edit_get_after(&$model){
        $model['group_id'] = db('AuthGroupAccess')->where('uid',$model['id'])->value('group_id');
        return ['err_code' => 0 , 'msg' => '默认通过'];
    }

    /**
     * 编辑前 返回 数组，如果code 等于 0 的 话 不继续
     */
    protected function handle_edit_post_before(&$param){
        // 密码为空说明不修改，去掉该项
        if(array_key_exists('admin_pwd',$param) && strlen($param['admin_pwd']) < 1){
            unset($param['admin_pwd']);
        }
        if(!empty($param['admin_pwd'])){
            $admin_pwd_salt = random(10);
            $param['admin_pwd_salt'] = $admin_pwd_salt;
            $param['admin_pwd'] = encrypt_password($param['admin_pwd'],$admin_pwd_salt);
        }
        return ['err_code' => 0 , 'msg' => '默认通过'];
    }

    /**
     * 编辑后
     */
    protected function handle_edit_post_after(&$param,$flag){
        if($flag['err_code'] === 0){
            // 编辑成功 添加分组数据
            $alj = new AdminLogic();
            $jieguo = $alj->_setAuthGroupAccess($param['id'],$param['group_id']);

            if($jieguo === false){
                return ['err_code' => 1 , 'msg' => '设置分组出错'];
            }
        }
        return ['err_code' => 0 , 'msg' => '默认通过'];
    }

    /**
     * 删除前检验，目前只检测，不能删除自己
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function handle_del_before($id,&$param){
        if ($id == $this->adminId) {
            return ['err_code' => 1 , 'msg' => '不能删除自己'];
        }
        return ['err_code' => 0 , 'msg' => '默认通过'];
    }

    /**
     * 编辑我的资料
     */
    public function profile(){
        $alj = new AdminLogic($this->modelName);
        if (request()->isAjax()){
            // 允许编辑的列
            $param = Request::only([
                'admin_pwd'=>'',
                'admin_pwd2'=>'',
                'admin_realname'=>'',
            ]);

            if(!empty($param['admin_pwd']) && $param['admin_pwd'] != $param['admin_pwd2']){
                $this->error('二次密码不一致');
            }
            if(!empty($param['admin_pwd'])){
                $admin_pwd_salt = random(10);
                $param['admin_pwd_salt'] = $admin_pwd_salt;
                $param['admin_changepwd'] = time();
                $param['admin_pwd'] = encrypt_password($param['admin_pwd'],$admin_pwd_salt);
                unset($param['admin_pwd2']);
            }
            $param['id'] = $this->adminId;
            $flag = $alj->save($param);

            if($flag['err_code'] === 0){
                $this->success($flag['msg']);
            }
            else{
                $this->error($flag['msg']);
            }
        }

        $jieguo = $alj->getModelByPk($this->adminId);
        if($jieguo['err_code'] > 0){
            $this->error($jieguo['msg']);
        }
        $aModel = $jieguo['data'];
        $this->assign('model',$aModel);
        return $this->fetch();
    }
}
